package com.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.engine.freemarker.FreemarkerEngine;
import com.common.*;
import com.entity.generate.FkBean;
import com.entity.generate.Table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Generator2{
    private Table table;
    private String templatePath;
    private String writePath;
    private String vuePath;
    private String dbName;
    private String tableName;
    private String packageName;
    private FreemarkerEngine freemarkerEngine;
    private Map<String,Object> bindMap;
    private Map<String,Object> reqMap;

    public Generator2(GeneratorTemplate generatorTemplate, MetaUtil metaUtil, Map<String, Object> map) {
        String tableName = (String) map.get("tableName");
        String dbName = (String) map.get("dbName");
        String templatePath = (String) map.get("templatePath");
        String writePath = (String) map.get("writePath");
        String vuePath = (String) map.get("vuePath");
        String packageName = (String) map.get("packageName");
        FreemarkerEngine freemarkerEngine = generatorTemplate.getFreemarkerEngine();
        Table tableMeta = metaUtil.getTableMeta(dbName, tableName);

        this.table = tableMeta;
        this.templatePath = templatePath;
        this.writePath = writePath;
        this.vuePath =vuePath;
        this.freemarkerEngine =freemarkerEngine;
        this.dbName = dbName;
        this.tableName = tableName;
        this.packageName = packageName;
        this.bindMap = getBindMap(tableMeta,metaUtil);
        this.reqMap = map;
    }

    private Map<String,Object> getBindMap(Table tableMeta,MetaUtil metaUtil){

        Map<String, Object> bindMap = new HashMap<>();
        bindMap.put("table",tableMeta);
        bindMap.put("toCamelCase",new FMCamelCase());
        bindMap.put("toUnderlineCase",new FMUnderlineCase());
        bindMap.put("getJavaType",new FMgetJavaType());
        bindMap.put("getJdbcType",new FMgetJdbcType());
        List<FkBean> fkList = tableMeta.getFkList();
        if(ObjectUtil.isNotEmpty(fkList) && fkList.size()>0){
            List<Table> list = new ArrayList<>();
            for (FkBean fkBean : fkList) {
                Table tableMeta1 = metaUtil.getTableMeta(dbName, fkBean.referencesTableName);
                list.add(tableMeta1);
            }
            bindMap.put("joinTable",list);
        }
        return bindMap;
    }

    private String getPath(String nameHeader){
        String className = StrUtil.toCamelCase("_" + tableName);
        String field = writePath + "\\entity\\field\\" + className + "Field.java";
        String serviceImpl = writePath + "\\service\\impl\\" + className + "ServiceImpl.java";
        String service = writePath + "\\service\\" + className + "Service.java";
        String entity = writePath + "\\entity\\" + className + "Entity.java";
        String dao = writePath + "\\dao\\" + className + "Dao.java";
        String resourcePath = FileUtil.getParent(writePath, 3) + "\\resources";
        String mapperXml = resourcePath + "\\mapper\\" + className + "Mapper.xml";
        String controller = writePath + "\\controller\\" + className + "Controller.java";

        String add = vuePath + "\\src\\pages\\"+className+"\\"+ className + "Add.vue";
        String edit = vuePath + "\\src\\pages\\"+className+"\\"+ className + "Edit.vue";
        String index = vuePath +"\\src\\pages\\"+className+"\\"+  className + "Index.vue";
        String indexJs = vuePath + "controller\\" + className + "Controller.java";

        if (nameHeader.contains("Controller")){
            return controller;
        }
        if (nameHeader.contains("Field")){
            return field;
        }
        if (nameHeader.contains("ServiceImpl")){
            return serviceImpl;
        }
        if (nameHeader.contains("Service")){
            return service;
        }
        if (nameHeader.contains("Entity")){
            return entity;
        }
        if (nameHeader.contains("Dao")){
            return dao;
        }
        if (nameHeader.contains("Mapper")){
            return mapperXml;
        }

        if (nameHeader.contains("Add")){
            return add;
        }

        if (nameHeader.contains("Edit")){
            return edit;
        }

        if (nameHeader.contains("Index")){
            return index;
        }

        if (nameHeader.contains("IndexJs")){
            return indexJs;
        }

        return "";
    }

    public void writeServiceFile(String nameHeader){
        String nameHeaderLowerFirst = StrUtil.lowerFirst(nameHeader);
        if(ObjectUtil.isNotEmpty(this.reqMap.get(nameHeader))){
            Template template = freemarkerEngine.getTemplate(nameHeaderLowerFirst + ".txt");
            String render = template.render(this.bindMap);
            String path = getPath(nameHeader);
            if(!path.equals("")){
                FileUtil.writeString(render, path, CharsetUtil.UTF_8);
            }
        }
    }

}
